<template>
  <h2>求和：{{ sum }}</h2>
  <button @click="sum++">sum++</button>
  <hr />
  <h2>姓名：{{ name }}</h2>
  <h2>年龄：{{ age }}</h2>
  <h2>职业：{{ job.j1.type }}</h2>
  <h2>薪资：{{ job.j1.salary }}K</h2>
  <button @click="name += '~'">修改姓名</button>
  <button @click="age++">修改年龄</button>
  <button @click="job.j1.salary++">加薪</button>
</template>
<script>
import { ref, reactive, toRefs,readonly,shallowReadonly } from "vue";
export default {
  name: "Demo",
  setup(props, context) {

    let sum = ref(0);
    let person = reactive({
      name: "张三",
      age: 18,
      job: {
        j1: {
          type: "前端",
          salary: 10,
        },
      },
    });
    /* 
    当一个响应式数据被readonly()包裹后，就变为只读数据，无法再进行更改
    如果操作，那么控制台会警告：Set operation on key "xxxx" failed: target is readonly.
    */
    // person= readonly(person)
    // sum = readonly(sum)

    /* 
    shallowReadonly()只将响应式对象中的第一层数据设置为只读，深层次数据依旧可以更改
    */
    // person = shallowReadonly(person)
    // sum = shallowReadonly(sum)

    // 返回一个对象供模版使用
    return {
      sum,
      ...toRefs(person),
    };
  },
};
</script>

